#ifndef LOADDATA_CPP_
#define LOADDATA_CPP_
#include "header.h"
int loadLatentFeatures(vector< vector<float> >& matrix, const char* srcFile)
{
    char rateStr[2000];
    char* pch;
    std::ifstream from (srcFile);
    if (!from.is_open()) {
        cout << "can't open input file:" << srcFile << "!\n";
        exit(1);
    }   
    int itemId = 1, itemNum = 0;
    float feature = 0.0;
    while(from.getline(rateStr,2000)){
        if (strlen(rateStr) < 3)continue;
        pch = strtok (rateStr, " ");
        if (pch != NULL) {
            itemId = atoi(pch);
        }
        pch = strtok (NULL, ",");
        int i = 0;
        while (pch != NULL) {
            feature = atof(pch);
            matrix[itemId].push_back(feature);
            ++i;
            if(i >= FEATURE_NUM) break;
            pch = strtok (NULL, ",");
        }
        ++itemNum;
    }
    from.close();
    return itemNum;
}

int loadTfidfFeatures(vector< map<int, float> >& matrix, const char* srcFile)
{
    char rateStr[2000];
    char* pch;
    std::ifstream from (srcFile);
    if (!from.is_open()) {
        cout << "can't open input file:" << srcFile << "!\n";
        exit(1);
    }   
    int itemId = 1, featureId = 1;
    float feature = 0.0;
    while(from.getline(rateStr,2000)){
        if (strlen(rateStr) < 3) continue;
        pch = strtok (rateStr, " ");
        int i = 0;
        while (pch != NULL) {
            if (0 == i) itemId = atoi(pch);
            else if (1 == i) featureId = atoi(pch);
            else if (2 == i) feature = atof(pch);
            else break;
            ++i;
            pch = strtok (NULL, " ");
        }
        matrix[itemId][featureId] = feature;
    }
    /*
    for (int i=1; i<=itemId; ++i) {
        map<int,float>::iterator it=matrix[i].begin();     
        for (; it!=matrix[i].end(); ++it) {     
            cout << i <<" " << it->first << " " << it->second << endl;
        }     
    }
    */
    return itemId;
}

int loadInLink(const char* inLinkFile, vector<int>& inlinks)
{
    char rateStr[100];
    char* pch;
    std::ifstream from (inLinkFile);
    if (!from.is_open()) {
        cout << "can't open input inlink file:" << inLinkFile << "!\n";
        exit(1);
    }   
    int itemId = 1, linkNum = 0, itemNum = 0;
    while (from.getline(rateStr, 100)) {
        if (strlen(rateStr) < 3)continue;
        pch = strtok (rateStr, " ");
        if (pch != NULL) {
            itemId = atoi(pch);
        }
        pch = strtok (NULL, " ");
        linkNum = atoi(pch);
        inlinks[itemId] = linkNum;
        ++itemNum;
    }
    from.close();
    return itemNum;
}

#endif //LODADATA_CPP_
